home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / IBNUM.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  237 lines

  1. /* Copyright (C) 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* ibnum.c */
  20. /* Level 2 encoded number reading utilities for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "stream.h"
  24. #include "bnum.h"
  25. #include "btoken.h"
  26.  
  27. /* ------ Encoded number reading ------ */
  28.  
  29. /* Set up to read from an encoded number array/string. */
  30. int
  31. sread_num_array(stream *s, ref *op)
  32. {    switch ( r_type(op) )
  33.        {
  34.     case t_string:
  35.        {    /* Check that this is a legitimate encoded number array */
  36.         byte *bp = op->value.bytes;
  37.         short count;
  38.         int nshift;
  39.         if ( r_size(op) < 4 || (bt_type)bp[0] != bt_num_array ||
  40.             !num_is_valid(bp[1])
  41.             )
  42.             return_error(e_typecheck);
  43.         sread_string(s, bp + 2, r_size(op) - 2);
  44.         s->num_format = bp[1];
  45.         sgetshort(s, &count);
  46.         nshift = ((bp[1] & 0x70) == 0x20 ? 1 : 2);
  47.         if ( count != (r_size(op) - 4) >> nshift )
  48.             return_error(e_typecheck);
  49.        }    break;
  50.     case t_array:
  51.         sread_string(s, (byte *)op->value.refs, r_size(op) * sizeof(ref));
  52.         s->num_format = num_array;
  53.         break;
  54.     default:
  55.         return_error(e_typecheck);
  56.        }
  57.     return 0;
  58. }
  59.  
  60. /* Get the number of elements in an encoded number stream. */
  61. uint
  62. scount_num_stream(stream *s)
  63. {    long avlong;
  64.     savailable(s, &avlong);
  65.     switch ( s->num_format & 0x170 )
  66.        {
  67.     case num_int16:
  68.         return (uint)(avlong >> 1);
  69.     case num_array:
  70.         return (uint)(avlong / sizeof(ref));
  71.     default:            /* num_int32, num_float */
  72.         return (uint)(avlong >> 2);
  73.        }
  74. }
  75.  
  76. /* Read an encoded number from a stream according to its num_format. */
  77. /* Put the value in np->value.{intval,realval}.  Return t_int, t_real, */
  78. /* t_null if end of stream, or e_syntaxerror or e_typecheck. */
  79. static double binary_scale[32] = {
  80. #define expn2(n) (0.5 / (1L << (n-1)))
  81.     1.0, expn2(1), expn2(2), expn2(3),
  82.     expn2(4), expn2(5), expn2(6), expn2(7),
  83.     expn2(8), expn2(9), expn2(10), expn2(11),
  84.     expn2(12), expn2(13), expn2(14), expn2(15),
  85.     expn2(16), expn2(17), expn2(18), expn2(19),
  86.     expn2(20), expn2(21), expn2(22), expn2(23),
  87.     expn2(24), expn2(25), expn2(26), expn2(27),
  88.     expn2(28), expn2(29), expn2(30), expn2(31)
  89. #undef expn2
  90. };
  91. int
  92. sget_encoded_number(stream *s, ref *np)
  93. {    int format = s->num_format;
  94.     short snum;
  95.     long lnum;
  96.     int code, type;
  97.     switch ( format & 0x170 )
  98.        {
  99.     case num_int32: case num_int32 + 16:
  100.         if ( (format & 31) == 0 )
  101.             type = t_integer,
  102.             code = sgetlong(s, &np->value.intval);
  103.         else
  104.            {    type = t_real;
  105.             code = sgetlong(s, &lnum);
  106.             if ( !code )
  107.                 np->value.realval =
  108.                   (double)lnum * binary_scale[format & 31];
  109.            }
  110.         break;
  111.     case num_int16:
  112.         code = sgetshort(s, &snum);
  113.         if ( (format & 15) == 0 )
  114.            {    type = t_integer;
  115.             np->value.intval = snum;
  116.            }
  117.         else
  118.            {    type = t_real;
  119.             if ( !code )
  120.                 np->value.realval =
  121.                   (double)snum * binary_scale[format & 15];
  122.            }
  123.         break;
  124.     case num_float:
  125.         type = t_real;
  126.         code = sgetfloat(s, &np->value.realval);
  127.         break;
  128.     case num_array:
  129.        {    uint count = sgets(s, (byte *)np, sizeof(ref));
  130.         code = (count == 0 ? 1 : count == sizeof(ref) ? 0 :
  131.             e_syntaxerror);
  132.         if ( !code )
  133.          switch ( r_type(np) )
  134.            {
  135.         case t_integer:
  136.             return t_integer;
  137.         case t_real:
  138.             return t_real;
  139.         default:
  140.             return_error(e_typecheck);
  141.            }
  142.        }    break;
  143.     default:
  144.         return_error(e_syntaxerror);    /* invalid num_format?? */
  145.        }
  146.     switch ( code )
  147.        {
  148.     case 0: return type;
  149.     case 1: return t_null;        /* end of stream */
  150.     default: return code;
  151.        }
  152. }
  153.  
  154. /* ------ Get/put number ------ */
  155.  
  156. /* Get/put encoded numbers on a stream according to num_format. */
  157. /* 1 means end of stream, 0 means not end, <0 means error. */
  158.  
  159. /* Get/put a short. */
  160. int
  161. sgetshort(register stream *s, short *p)
  162. {    int a = sgetc(s), b;
  163.     if ( a < 0 )
  164.         return 1;
  165.     b = sgetc(s);
  166.     if ( b < 0 )
  167.         return_error(e_syntaxerror);
  168.     *p = (short)(s_is_lsb(s) ? (b << 8) + a : (a << 8) + b);
  169.     return 0;
  170. }
  171. void
  172. sputshort(register stream *s, short num)
  173. {    byte a = num & 0xff;
  174.     byte b = (byte)(num >> 8);
  175.     if ( s_is_msb(s) )
  176.        {    byte t = a; a = b; b = t;
  177.        }
  178.     sputc(s, a);
  179.     sputc(s, b);
  180. }
  181.  
  182. /* Get/put a long. */
  183. int
  184. sgetlong(register stream *s, long *p)
  185. {    int a = sgetc(s), b, c, d;
  186.     if ( a < 0 ) return 1;
  187.     b = sgetc(s);
  188.     c = sgetc(s);
  189.     d = sgetc(s);
  190.     if ( (b | c | d) < 0 )
  191.         return_error(e_syntaxerror);
  192.     *p =  (long)(s_is_lsb(s) ?
  193.              ((long)d << 24) + ((long)c << 16) + (b << 8) + a :
  194.              ((long)a << 24) + ((long)b << 16) + (c << 8) + d);
  195.     return 0;
  196. }
  197. void
  198. sputlong(register stream *s, long num)
  199. {    byte a = num & 0xff;
  200.     byte b = (byte)(num >> 8);
  201.     byte c = (byte)(num >> 16);
  202.     byte d = (byte)(num >> 24);
  203.     if ( s_is_msb(s) )
  204.        {    byte t = a; a = d; d = t;
  205.         t = b; b = c; c = t;
  206.        }
  207.     sputc(s, a);
  208.     sputc(s, b);
  209.     sputc(s, c);
  210.     sputc(s, d);
  211. }
  212.  
  213. /* Get/put a float.  We don't handle non-IEEE native representations yet. */
  214. int
  215. sgetfloat(register stream *s, float *p)
  216. {    if ( s->num_format == num_float_native )
  217.        {    uint len = sgets(s, (byte *)p, sizeof(float));
  218.         return (len == sizeof(float) ? 0 :
  219.             len == 0 && seofp(s) ? 1 :
  220.             e_syntaxerror);
  221.        }
  222.     else
  223.        {    /* Hack: we know floats and longs are the same size. */
  224.         return sgetlong(s, (long *)p);
  225.        }
  226. }
  227. void
  228. sputfloat(register stream *s, floatp num)
  229. {    float f = num;            /* coerce from double */
  230.     if ( s->num_format == num_float_native )
  231.         sputs(s, (byte *)&f, sizeof(float));
  232.     else
  233.        {    /* Hack: we know floats and longs are the same size. */
  234.         sputlong(s, *(long *)&f);
  235.        }
  236. }
  237.